home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / test_math.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  6.0 KB  |  135 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. *
  5. print 'math module, testing with eps', seps
  6. import math
  7.  
  8. def testit(name, value, expected):
  9.     if abs(value - expected) > eps:
  10.         raise TestFailed, '%s returned %f, expected %f' % (name, value, expected)
  11.     
  12.  
  13. print 'constants'
  14. testit('pi', math.pi, 3.1415926)
  15. testit('e', math.e, 2.7182818)
  16. print 'acos'
  17. testit('acos(-1)', math.acos(-1), math.pi)
  18. testit('acos(0)', math.acos(0), math.pi / 2)
  19. testit('acos(1)', math.acos(1), 0)
  20. print 'asin'
  21. testit('asin(-1)', math.asin(-1), -(math.pi) / 2)
  22. testit('asin(0)', math.asin(0), 0)
  23. testit('asin(1)', math.asin(1), math.pi / 2)
  24. print 'atan'
  25. testit('atan(-1)', math.atan(-1), -(math.pi) / 4)
  26. testit('atan(0)', math.atan(0), 0)
  27. testit('atan(1)', math.atan(1), math.pi / 4)
  28. print 'atan2'
  29. testit('atan2(-1, 0)', math.atan2(-1, 0), -(math.pi) / 2)
  30. testit('atan2(-1, 1)', math.atan2(-1, 1), -(math.pi) / 4)
  31. testit('atan2(0, 1)', math.atan2(0, 1), 0)
  32. testit('atan2(1, 1)', math.atan2(1, 1), math.pi / 4)
  33. testit('atan2(1, 0)', math.atan2(1, 0), math.pi / 2)
  34. print 'ceil'
  35. testit('ceil(0.5)', math.ceil(0.5), 1)
  36. testit('ceil(1.0)', math.ceil(1.0), 1)
  37. testit('ceil(1.5)', math.ceil(1.5), 2)
  38. testit('ceil(-0.5)', math.ceil(-0.5), 0)
  39. testit('ceil(-1.0)', math.ceil(-1.0), -1)
  40. testit('ceil(-1.5)', math.ceil(-1.5), -1)
  41. print 'cos'
  42. testit('cos(-pi/2)', math.cos(-(math.pi) / 2), 0)
  43. testit('cos(0)', math.cos(0), 1)
  44. testit('cos(pi/2)', math.cos(math.pi / 2), 0)
  45. testit('cos(pi)', math.cos(math.pi), -1)
  46. print 'cosh'
  47. testit('cosh(0)', math.cosh(0), 1)
  48. testit('cosh(2)-2*cosh(1)**2', math.cosh(2) - 2 * math.cosh(1) ** 2, -1)
  49. print 'exp'
  50. testit('exp(-1)', math.exp(-1), 1 / math.e)
  51. testit('exp(0)', math.exp(0), 1)
  52. testit('exp(1)', math.exp(1), math.e)
  53. print 'fabs'
  54. testit('fabs(-1)', math.fabs(-1), 1)
  55. testit('fabs(0)', math.fabs(0), 0)
  56. testit('fabs(1)', math.fabs(1), 1)
  57. print 'floor'
  58. testit('floor(0.5)', math.floor(0.5), 0)
  59. testit('floor(1.0)', math.floor(1.0), 1)
  60. testit('floor(1.5)', math.floor(1.5), 1)
  61. testit('floor(-0.5)', math.floor(-0.5), -1)
  62. testit('floor(-1.0)', math.floor(-1.0), -1)
  63. testit('floor(-1.5)', math.floor(-1.5), -2)
  64. print 'fmod'
  65. testit('fmod(10,1)', math.fmod(10, 1), 0)
  66. testit('fmod(10,0.5)', math.fmod(10, 0.5), 0)
  67. testit('fmod(10,1.5)', math.fmod(10, 1.5), 1)
  68. testit('fmod(-10,1)', math.fmod(-10, 1), 0)
  69. testit('fmod(-10,0.5)', math.fmod(-10, 0.5), 0)
  70. testit('fmod(-10,1.5)', math.fmod(-10, 1.5), -1)
  71. print 'frexp'
  72.  
  73. def testfrexp(name, .2, .4):
  74.     (mant, exp) = .2
  75.     (emant, eexp) = .4
  76.     if abs(mant - emant) > eps or exp != eexp:
  77.         raise TestFailed, '%s returned %s, expected %s' % (name, `(mant, exp)`, `(emant, eexp)`)
  78.     
  79.  
  80. testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
  81. testfrexp('frexp(0)', math.frexp(0), (0, 0))
  82. testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
  83. testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
  84. print 'hypot'
  85. testit('hypot(0,0)', math.hypot(0, 0), 0)
  86. testit('hypot(3,4)', math.hypot(3, 4), 5)
  87. print 'ldexp'
  88. testit('ldexp(0,1)', math.ldexp(0, 1), 0)
  89. testit('ldexp(1,1)', math.ldexp(1, 1), 2)
  90. testit('ldexp(1,-1)', math.ldexp(1, -1), 0.5)
  91. testit('ldexp(-1,1)', math.ldexp(-1, 1), -2)
  92. print 'log'
  93. testit('log(1/e)', math.log(1 / math.e), -1)
  94. testit('log(1)', math.log(1), 0)
  95. testit('log(e)', math.log(math.e), 1)
  96. print 'log10'
  97. testit('log10(0.1)', math.log10(0.1), -1)
  98. testit('log10(1)', math.log10(1), 0)
  99. testit('log10(10)', math.log10(10), 1)
  100. print 'modf'
  101.  
  102. def testmodf(name, .2, .4):
  103.     (v1, v2) = .2
  104.     (e1, e2) = .4
  105.     if abs(v1 - e1) > eps or abs(v2 - e2):
  106.         raise TestFailed, '%s returned %s, expected %s' % (name, `(v1, v2)`, `(e1, e2)`)
  107.     
  108.  
  109. testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
  110. testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
  111. print 'pow'
  112. testit('pow(0,1)', math.pow(0, 1), 0)
  113. testit('pow(1,0)', math.pow(1, 0), 1)
  114. testit('pow(2,1)', math.pow(2, 1), 2)
  115. testit('pow(2,-1)', math.pow(2, -1), 0.5)
  116. print 'sin'
  117. testit('sin(0)', math.sin(0), 0)
  118. testit('sin(pi/2)', math.sin(math.pi / 2), 1)
  119. testit('sin(-pi/2)', math.sin(-(math.pi) / 2), -1)
  120. print 'sinh'
  121. testit('sinh(0)', math.sinh(0), 0)
  122. testit('sinh(1)**2-cosh(1)**2', math.sinh(1) ** 2 - math.cosh(1) ** 2, -1)
  123. testit('sinh(1)+sinh(-1)', math.sinh(1) + math.sinh(-1), 0)
  124. print 'sqrt'
  125. testit('sqrt(0)', math.sqrt(0), 0)
  126. testit('sqrt(1)', math.sqrt(1), 1)
  127. testit('sqrt(4)', math.sqrt(4), 2)
  128. print 'tan'
  129. testit('tan(0)', math.tan(0), 0)
  130. testit('tan(pi/4)', math.tan(math.pi / 4), 1)
  131. testit('tan(-pi/4)', math.tan(-(math.pi) / 4), -1)
  132. print 'tanh'
  133. testit('tanh(0)', math.tanh(0), 0)
  134. testit('tanh(1)+tanh(-1)', math.tanh(1) + math.tanh(-1), 0)
  135.